package com.javaEE.gamepigeon.controller;



import com.javaEE.gamepigeon.entity.Group;
import com.javaEE.gamepigeon.entity.UserFavorite;
import com.javaEE.gamepigeon.service.AttentionService;
import com.javaEE.gamepigeon.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "收藏关注竞赛")
@RestController
@RequestMapping("/attention")
public class AttentionController {

    @Autowired
    AttentionService attentionService;


    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户的Id",paramType = "query"),
            @ApiImplicitParam(name="comId",value="竞赛的Id",paramType = "query"),
            @ApiImplicitParam(name="weight",value = "权重",paramType = "query"),
            @ApiImplicitParam(name="note",value = "记号",paramType = "query"),
            @ApiImplicitParam(name="groupId",value = "组id",paramType = "query")
    })
    @ApiOperation("用户添加收藏")
    @GetMapping("/addFavorite")
    public Result addFavorite(Integer userId, Integer comId, @RequestParam(required = false) Integer weight, @RequestParam(required = false) String note, @RequestParam(required = false) Integer groupId){
        if(attentionService.addFavorite(userId,comId,weight,note,groupId)){
            return Result.success("ok");
        }else {
            return Result.error("添加失败");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户的Id",paramType = "query"),
            @ApiImplicitParam(name="comId",value="比赛的Id",paramType = "query"),
    })
    @ApiOperation("用户移除收藏")
    @GetMapping("/deleteFavorite")
    public Result deleteFavorite(Integer userId, Integer comId){
        if(attentionService.deleteFavorite(userId,comId)){
            return Result.success("ok");
        }else {
            return Result.error("移除失败");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户的Id",paramType = "query"),
    })
    @ApiOperation("查询用户关注(所有关注)")
    @GetMapping("/queryFavorite")
    public List<UserFavorite> queryFavorite(Integer userId){
        List<UserFavorite> list= attentionService.queryFavorite(userId);
        if(list!=null){
            list = list.stream().peek(a->a.getCompetitionBrief().setInAttention(true)).collect(Collectors.toList());
        }
        return list;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户的Id",paramType = "query"),
            @ApiImplicitParam(name = "name",value = "组名",paramType = "query")
    })
    @ApiOperation("添加用户的分组")
    @PostMapping("/addGroup")
    public Result addGroup(Integer userId, String name){
        if(attentionService.addGroup(userId,name)){
            return Result.success("ok");
        }else {
            return Result.error("添加失败");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户的Id",paramType = "query")
    })
    @ApiOperation("查询用户的分组")
    @GetMapping("/queryGroup")
    public List<Group> queryGroup(Integer userId){
        return attentionService.queryGroup(userId);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId",value = "分组的Id",paramType = "query")
    })
    @ApiOperation("根据groupId删除用户分组")
    @GetMapping("/deleteGroup")
    public Result deleteGroup(Integer groupId){
        if(attentionService.deleteGroup(groupId)){
            return Result.success("ok");
        }else {
            return Result.error("删除失败");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId",value = "分组的Id",paramType = "query"),
            @ApiImplicitParam(name = "groupName",value = "分组的新名字",paramType = "query")
    })
    @ApiOperation("更新用户分组名")
    @GetMapping("/updateGroup")
    public Result updateGroup(Integer groupId, String groupName){
        if(attentionService.updateGroup(groupId,groupName)){
            return Result.success("ok");
        }else {
            return Result.error("更新失败");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name="groupId",value = "分组的Id",paramType = "query")
    })
    @ApiOperation("查询对应分组Id下用户的竞赛")
    @GetMapping("/getCompetition")
    public List<UserFavorite> getCompetition(Integer groupId){
        List<UserFavorite> list = attentionService.getCompetition(groupId);
        if(list!=null){
            list = list.stream().peek(a->a.getCompetitionBrief().setInAttention(true)).collect(Collectors.toList());
        }
        return list;
    }
}
